{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import html\n",
    "import re\n",
    "from fontTools.ttLib import TTFont"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\uXXXX escape (<ipython-input-7-ac5d45ee947d>, line 35)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-7-ac5d45ee947d>\"\u001b[0;36m, line \u001b[0;32m35\u001b[0m\n\u001b[0;31m    utf8List = [eval(\"u'\\u\" + uni[3:] + \"'\").encode(\"utf-8\") for uni in uniList[1:]]\u001b[0m\n\u001b[0m                    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\uXXXX escape\n"
     ]
    }
   ],
   "source": [
    "#抓取autohome评论\n",
    "class AutoSpider:\n",
    "    #页面初始化\n",
    "    def __init__(self):\n",
    "        self.headers = {\n",
    "            \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8\",\n",
    "            \"Accept-Encoding\": \"gzip, deflate, br\",\n",
    "            \"Accept-Language\": \"zh-CN,zh;q=0.8\",\n",
    "            \"Cache-Control\": \"max-age=0\",\n",
    "            \"Connection\": \"keep-alive\",\n",
    "            \"Upgrade-Insecure-Requests\": \"1\",\n",
    "            \"Content-Type\": \"application/x-www-form-urlencoded; charset=UTF-8\",\n",
    "            \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36\"\n",
    "        }\n",
    "    # 获取评论\n",
    "    def getNote(self):\n",
    "        url = \"https://club.autohome.com.cn/bbs/thread-c-2778-69436529-1.html\"\n",
    "        host = {'host':'club.autohome.com.cn',\n",
    "                'cookie':'your cookie'}\n",
    "        headers = dict(self.headers.items() + host.items())\n",
    "        # 获取页面内容\n",
    "        r = requests.get(url, headers=headers)\n",
    "        response = html.fromstring(r.text)\n",
    "        # 匹配ttf font\n",
    "        cmp = re.compile(\",url\\('(//.*.ttf)'\\) format\\('woff'\\)\")\n",
    "        rst = cmp.findall(r.text)\n",
    "        ttf = requests.get(\"http:\" + rst[0], stream=True)\n",
    "        with open(\"autohome.ttf\", \"wb\") as pdf:\n",
    "            for chunk in ttf.iter_content(chunk_size=1024):\n",
    "                if chunk:\n",
    "                    pdf.write(chunk)\n",
    "        # 解析字体库font文件\n",
    "        font = TTFont('autohome.ttf')\n",
    "        uniList = font['cmap'].tables[0].ttFont.getGlyphOrder()\n",
    "        utf8List = [eval(\"u'\\u\" + uni[3:] + \"'\").encode(\"utf-8\") for uni in uniList[1:]]\n",
    "        wordList = ['一', '七', '三', '上', '下', '不', '中', '档', '比', '油', '泥', '灯',\n",
    "                    '九', '了', '二', '五', '低', '保', '光', '八', '公', '六', '养', '内', '冷',\n",
    "                    '副', '加', '动', '十', '电', '的', '皮', '盘', '真', '着', '路', '身', '软',\n",
    "                    '过', '近', '远', '里', '量', '长', '门', '问', '只', '右', '启', '呢', '味',\n",
    "                    '和', '响', '四', '地', '坏', '坐', '外', '多', '大', '好', '孩', '实', '小',\n",
    "                    '少', '短', '矮', '硬', '空', '级', '耗', '雨', '音', '高', '左', '开', '当',\n",
    "                    '很', '得', '性', '自', '手', '排', '控', '无', '是', '更', '有', '机', '来']\n",
    "        # 获取发帖内容\n",
    "        note = response.cssselect(\".tz-paragraph\")[0].text_content().encode('utf-8')\n",
    "        print(note)\n",
    "        print('---------------after-----------------')\n",
    "        for i in range(len(utf8List)):\n",
    "            note = note.replace(utf8List[i], wordList[i])\n",
    "        print(note)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
